#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2020/12/04 13:50
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：
import logging

TABLE_DICT = {
        'TableConfig':
            """CREATE TABLE TABLECONFIG (
    ID INTEGER DEFAULT 0,
    TABLENAME TEXT (50),
    TAGSOURCE TEXT (50)
)""",
        'ConnectConfig':
            """CREATE TABLE CONNECTCONFIG (
    NAME  TEXT(20) NOT NULL,
    TAGSOURCE  TEXT(20),
    TYPE  TEXT(20),
    MODE  TEXT(20),
    HOST  TEXT(100),
    PORT  INTEGER,
    USERNAME  TEXT(50),
    PASSWORD  TEXT(50),
    DATABASE  TEXT(50),
    DSN  TEXT(50),
    MINCACHED  INTEGER DEFAULT 2,
    MAXCONNECTIONS  INTEGER DEFAULT 5,
    MAXCACHED  INTEGER DEFAULT 1,
    MAXSHARED  INTEGER DEFAULT 0,
    DATABASEPATH  TEXT(100),
    FORMAT  TEXT(20),
    LEFT  TEXT(20),
    RIGHT  TEXT(20),
    PRIMARY KEY (NAME ASC)
)""",
        'LastMark':
            """CREATE TABLE LASTMARK (
    MARK  TEXT(50),
    UPDATETIME  TEXT(20),
    TASKNAME  TEXT(200)
)""",
        'SchedulerConfig':
            """CREATE TABLE SCHEDULERCONFIG (
    ID INTEGER DEFAULT 1,
    DESCRIPTION TEXT(100),
    NAME TEXT(50),
    CYCLE TEXT(10) DEFAULT 7,
    AUTOSTART INTEGER DEFAULT 0,
    START INTEGER DEFAULT 0,
    STOP INTEGER DEFAULT 1,
    TAGCONNECTNAME TEXT(30),
    SOURCECONNECTNAME TEXT(30),
    DELTASK INTEGER,
    MARKFIELDTYPE TEXT(1),
    SERIALFIELD TEXT(50),
    MARKFIELD TEXT(50),
    MARKFIELDINITVALUE TEXT(100),
    MARKFIELDENDVALUE TEXT(100),
    OTHER TEXT(200),
    DATEFIELD TEXT(50),
    MODE TEXT(2)
)""",
        'FieldConfig':
            """CREATE TABLE FIELDCONFIG (
    ID INTEGER DEFAULT 1,
    DESCRIPTION TEXT(50),
    TABLENAME TEXT(50),
    FIELDNAME TEXT(50),
    DEFAULTVALUE TEXT(100),
    TARGETFIELD TEXT(100),
    TARGETTABLE TEXT(50),
    MODE TEXT(2)
)""",
        'LogConfig':
            """CREATE TABLE LOGCONFIG (
    LOGLEVEL INTEGER DEFAULT 10,
    LOGSAVE TEXT (2) DEFAULT 1,
    LOGSAVEPATH TEXT (100) DEFAULT './LOG',
    LOGSAVECYCLE TEXT (1) DEFAULT 8,
    LOGSAVEUNIT TEXT (5) DEFAULT 'H',
    LOGSAVENUMBER INTEGER DEFAULT 168,
    LOGSAVESIZE INTEGER DEFAULT 5,
    LOGSAVEDATABASE INTEGER DEFAULT 0,
    LOGDATABASECONNECTNAME TEXT (50)
)""",
        'register':
            """CREATE TABLE REGISTER (
    SN TEXT (100),
    PASSWORD TEXT (500)
)""",
        'APPConfig':
            """CREATE TABLE APPCONFIG (
    APPID TEXT (20) NOT NULL DEFAULT 1234567,
    APPNAME TEXT (50),
    DEVICEID TEXT (50),
    STARTSDK INTEGER DEFAULT 0,
    ENABLEPASSWORD INTEGER DEFAULT 1,
    PRIMARY KEY (APPID ASC)
)"""
        }


class CheckTable(object):
    """docstring for CheckTable"""

    def __init__(self, local_connect):
        super(CheckTable, self).__init__()
        self.local_connect = local_connect
        self.log = logging.getLogger('main.check_local_database')
        self.check_table()

    @staticmethod
    def add_field(self, table_name, field_name, field_type = None, field_lenght = 36, field_default = 0):
        if field_type:
            sql = "ALTER TABLE {} ADD COLUMN {} TEXT ({}) DEFAULT '{}';".format(table_name, field_name, field_lenght,
                                                                                field_default)
        else:
            sql = "ALTER TABLE {} ADD COLUMN {} INTEGER DEFAULT {};".format(table_name, field_name, field_default)
        self.log.info("增加字段:{},默认值:{}".format(field_name, field_default))

        return self.execSql(sql)

    def check_table(self):
        for name, creat_sql in TABLE_DICT.items():
            sql = "SELECT * FROM SQLITE_MASTER WHERE TYPE='table' AND NAME='{}'".format(name)
            if not self.local_connect.loadSql(sql):
                self.log.warning("表:{},不存在,创建表:{}".format(name, creat_sql))
                self.local_connect.execSql(creat_sql)